import { Redirect, useAccess, useModel, useLocation, Access } from 'umi';
import { ReactElement, FC } from 'react';

const whiteList = ['/404', '/401', '/500'];
interface IProps {
  children: ReactElement;
}
const Auth: FC<IProps> = ({ children }) => {
  const { initialState } = useModel('@@initialState');
  const location = useLocation();
  const { permissions } = useAccess();
  if (!initialState?.isLogin) {
    return <Redirect to="/userlogin" />;
  }
  const accessible =
    permissions.includes(location.pathname) || whiteList.includes(location.pathname);
  return (
    <Access accessible={accessible} fallback={<Redirect to="/401" />}>
      {children}
    </Access>
  );
};
export default Auth;
